// pages/service/bankcard-binding/index.js
var app=getApp(),api=require('../../../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankArr: [
      {
        id: 0,
        name: '中国银行'
      },
      {
        id: 0,
        name: '工商银行'
      }
    ],
    bank:"",
    bankaccount :"",
    bankname:""
  },
  bindBankChange: function (e) {
    var that = this;
    that.setData({
      bank: that.data.bankArr[e.detail.value]
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "bank") {
      console.log(inputVlaue)
      that.setData({
        bank: inputVlaue
      });
    }
    if (inputName == "bankName") {
      that.setData({
        bankname: inputVlaue
      });
    }
    if (inputName == "bankAccount") {
      that.setData({
        bankaccount: inputVlaue
      });
    }
  },
  onShow:function(){
    var that=this
    app.request({
      url: api.card.list,
      data:{
        bid:wx.getStorageSync('bid')
      },
      success:function(e){
        console.log(e)
        that.setData({
          bankArr:e.data
        })
      }
    })
  
  },
  sendRequest: function () {
    var that = this;
   
    if (that.validate()) {
      // TODO 发送请求
     app.request({
       url: api.card.add,
       method:'post',
       data:{
         uid:wx.getStorageSync('uid'),
         cid: that.data.bank.id,
         name: that.data.bankname,
         cardnumber: that.data.bankaccount
       },
       success:function(e){
        if(e.data==1){
          wx.showToast({
            title: '提交成功',
            duration:1000,
            success:function(e){
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1
                })
              },1000)
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: '提交失败',
            showCancel:false
          })
        }
       }
     })
     
    }
  },
  validate: function () {
    var that = this;

    if (!that.data.bank || that.data.bank == '') {
      wx.showToast({
        title: '请选择开户行',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.bankname || that.data.bankname == '') {
      wx.showToast({
        title: '开户人不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.bankaccount || that.data.bankaccount == '') {
      wx.showToast({
        title: '账号不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  },
 
})